﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Linq;

namespace zframe {
    public class FileUtils
    {
        /// <summary>
        /// 把unity工程Assets下的相对路径转换成绝对路径
        /// </summary>
        /// <param name="relativePath"></param>
        /// <returns></returns>
        public static string RelativeToAbsolute(string relativePath) {

            string[] paths = PathToArray(relativePath);
            paths = paths.Where(s  => s != "Assets").ToArray();
            string temp = Path.Combine(paths);
            string absolutePath = Path.Combine(Application.dataPath, temp);
            return absolutePath;
        }

        /// <summary>
        /// 获取目录路径最后一个文件夹的名称
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static string GetDirLastFolderName(string dirPath)
        {
            string[] dirArr = PathToArray(dirPath);
            return dirArr[dirArr.Length - 1];
        }

        public static string GetFileLastFolderName(string filePath)
        {
            string[] fileArr = PathToArray(filePath);
            fileArr = fileArr.Where(x => x != fileArr[fileArr.Length - 1]).ToArray();
            return fileArr[fileArr.Length-1];
        }


        public static string[] PathToArray(string path) {
            string[] dirArr = path.Split(new char[] { '\\', '/' });
            return dirArr;
        }

        public static string[] GetFilesByExts(string dir, string[] exts)
        {
            string[] files = null;
            if (Directory.Exists(dir))
            {
                files = Directory.GetFiles(dir);//传入绝对路径得到的是绝对路径,传入相对路径得到的就是相对路径
                files = files.Where(f=> exts.Contains(Path.GetExtension(f).Trim())).ToArray(); 
            }
            return files;
        }

    }
}